<?php
/**
 * Enhanced view handler
 * 
 * Based on the code given by http: *blog.astrumfutura.com/archives/288-Complex-Views-with-the-Zend-Framework-Part-5-The-Two-Step-View-Pattern.html
 * 
 * @package	Nuclide-Mvc
 * @version $Id$
 */

Zend_Loader::loadClass('Zend_View_Interface');
Zend_Loader::loadCLass('Zend_View');

/**
 * Enhanced view handler
 * 
 * Based on the code given by http: *blog.astrumfutura.com/archives/288-Complex-Views-with-the-Zend-Framework-Part-5-The-Two-Step-View-Pattern.html
 * 
 * @package	Nuclide-Mvc
 */
class nuc_View extends Zend_View implements Zend_View_Interface
{
	/**
	 * CSS files to be included first should get this order value
	 */
	const ORDER_FIRST = 0;
	/**
	 * CSS files to be included last should get this order value
	 */	
	const ORDER_LAST = 65536;
	/**
	 * The view that the controller really want to render.
	 * 
	 * @var string
	 */
	protected $_contentFile = null;
    /**
     * The layout view file.
     *
     * @var string
     */
    protected $_layoutFile = null;
    
    /**
     * Render the two-step view.
     *
     * @param string $name 
     * @return string
     */
    public function render($name) {
        if( $this->hasLayout() && !isset( $this->_contentFile ) ) {
            $this->_contentFile = $name;
            return parent::render( $this->getLayout() );
        }
        return parent::render( $name );
    }

    /**
     * Set layout file. Chainable.
     * 
     * @param	string			View file
     * @return	Zend_View_Abstract
     */
    public function setLayout( $file ) {
        $this->_layoutFile = $file;
        
        return $this;
    }

    /**
     * Get layout file.
     *
     * @return	string
     */
    public function getLayout() {
        return $this->_layoutFile;
    }

    /**
     * Do we have a layout?
     *
     * @return	bool
     */
    public function hasLayout() {
        return isset( $this->_layoutFile );
    }

    /**
     * Render the desired view in the layout.
     *
     * @return	Layout contents
     * @throws	Exception
     */
    protected function content() {
        if( !isset( $this->_contentFile) ) {
	        throw new Exception('No layout view to render.');
        }
        
        return parent::render( $this->_contentFile );
    }
		
	/**
	 * Store Javascript files in a common placeholder
	 *
	 * @param	string		Script path
	 * @param	int			OPTIONAL Script order number
	 * @param	string		OPTIONAL Section name
	 */	
	public function includeJs( $path, $order = 5, $section = 'default' ) {
		if( !isset( $this->placeholder('javascript')->section ) ) {
			$this->placeholder('javascript')->section = array();
		}
	
		if( !isset( $this->placeholder('javascript')->section ) || !isset( $this->placeholder('javascript')->section[ $section ] ) ) {
			$this->placeholder('javascript')->section[ $section ] = array();
		}
		
		$baseUrl = !is_null( $this->base_url ) ? $this->base_url : $this->placeholder('system')->base_url;
		$this->placeholder('javascript')->section[ $section ][$baseUrl . $path] = array(
			'path' => $baseUrl . $path,
			'order' => $order
		);
	}
	
	/**
	 * Fetches all assigned Javascript files
	 *
	 * @param	string			Section name
	 * @return	array of files
	 */		
	public function getJs( $section = 'default' ) {
		if( !isset( $this->placeholder('javascript')->section ) || !isset( $this->placeholder('javascript')->section[ $section ] ) ) {
			return array();
		}
		
		$order = array();
		$data = array();
		
		foreach( $this->placeholder('javascript')->section[ $section ] as $key => $row ) {
			$order[$key] = $row['order'];
			$data[$key] = $row['path'];
		}

		array_multisort( $order, SORT_ASC, $data );

		return $data;
	}
	
	/**
	 * Store stylesheet files in a common placeholder
	 *
	 * @param	string path to stylesheet file
	 */		
	public function includeCss( $path, $order = 5 ) {
		if( !isset( $this->placeholder('css')->default ) ) {
			$this->placeholder('css')->default = array();
		}
		
		$baseUrl = isset( $this->base_url ) ? $this->base_url : $this->placeholder('system')->base_url;
		$this->placeholder('css')->default[ $baseUrl . $path ] = array(
			'path'	=> $baseUrl . $path,
			'order'	=> $order
		);
	}

	/**
	 * Fetches all assigned stylesheet files
	 *
	 * @return	array of files
	 */	
	public function getCss() {
		if( !isset( $this->placeholder('css')->default ) ) {
			return array();
		}
				
		$order = array();
		$data = array();		
		
		foreach( $this->placeholder('css')->default as $key => $row ) {
			$order[ $key ] = $row['order'];
			$data[ $key ] = $row['path'];
		}

		array_multisort( $order, SORT_ASC, $data );

		return $data;
	}	
}
?>